/**  
 * All rights Reserved, Designed By www.flksh.com
 * @date:   2017-10-26 13:28:53   
 * @Copyright: 2013-2017 www.flksh.com Inc. All rights reserved. 
 * 注意：本内容仅限于上海福乐康社区养老服务有限公司内部传阅，禁止外泄以及用于其他的商业目
 */
package com.zx.cloud.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zx.cloud.domain.Dict;
import com.zx.cloud.persistence.CrudMapper;
import com.zx.cloud.persistence.DictMapper;
import com.zx.cloud.util.Page;

/**
 * 
 */
@Service
@Transactional
public class DictService extends CrudServiceImpl<Dict> {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(DictService.class);

	@Autowired
	private DictMapper dictMapper;

	@Autowired
	public DictService(CrudMapper<Dict> mapper) {
		super(mapper);
	}
	
	/**
	 * @param gmc
	 * @param 查询字典
	 * @return
	 */
	public List<Dict> findDictList(Dict dict){
		
		return dictMapper.queryList(dict);
	}

	public Page<Dict> page(Dict dict) {
		dict.setOrderBy("id desc");
		long count = mapper.count(dict);
		Page<Dict> page = new Page<Dict>(dict.getPageIndex_(),
				dict.getPageSize_());
		page.setData(mapper.find(dict));
		page.setTotalCount(count);
		return page;
	}

	public int update(Dict dict) {
		// 判断进入删除
		if (dict.getStatus() == 1) {
			Dict dictEntity = new Dict();
			dictEntity.setId(dict.getId());

			// 判断是否父级
			List<Dict> dictList = dictMapper.find(dictEntity);
			if (dictList.get(0).getParentCode().equals("0")) {
				dictEntity = new Dict();
				dictEntity.setStatus(0);
				dictEntity.setParentCode(dictList.get(0).getCode());
				// 判断是否有子级
				if (dictMapper.find(dictEntity).size() > 0) {
					// 先删子级
					return 2;
				}
			}
		}
		
		int success = dictMapper.update(dict);
		return success;
	}
	/**
	 * @param gmc
	 * @param 查询父类字典信息
	 * @param dict 字典实体
	 * @return 
	 */
	public Page<Dict> queryPageByParent(Dict dict) {
		long count = dictMapper.queryByParentCount(dict);
		Page<Dict> page = new Page<Dict>(dict.getPageIndex_(),
				dict.getPageSize_());
		page.setData(dictMapper.queryByParentCode(dict));
		page.setTotalCount(count);
		return page;
	}
	
    /**
     * @Description:获取利率
     * @author likun
     * @Create Time: 2017年11月6日上午9:21:17
     * @param dict
     * @return
     */
	public List<Dict>  getDitRate(Dict dict){
		Dict newDict=new Dict();
		newDict.setParentCode(dict.getParentCode());
		return dictMapper.getDitByParentCode(dict);
	}
	
	public Map<String, String> getSmsDataConfig(int type){
		
		Map<String, String> params = new HashMap<String, String>();
		
		//区分接口短信和营销短信
		String filterCode = null;
		if(type == 0){
			filterCode = "SMS_SIGAN_MARKET";
		}else{
			filterCode = "SMS_SIGAN_SINGLE";
		}
		/*
		 * 获取数据库短信接口相关配置
		 */
		Dict dict = new Dict();
		dict.setParentCode("SMS0001");
		List<Dict> dicts = dictMapper.getDitByParentCode(dict);
		if(null != dicts && dicts.size() > 0){
			for(int i = 0; i < dicts.size(); i++){
				if(!filterCode.equals(dicts.get(i).getCode())){
					params.put(dicts.get(i).getName(), dicts.get(i).getValue());
				}
			}
		}
		
		return params;
	}
	
	public List<Dict> getBanks(){
		
		Dict dict = new Dict();
		dict.setParentCode("BANK");
		List<Dict> dicts = dictMapper.getDitByParentCode(dict);
		
		return dicts;
	}
}
